/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 14-6-6
 * Time: 下午3:11
 * To change this template use File | Settings | File Templates.
 */
define(function(require,exports,module){
    var VERSION = '1.0',
        jQuery = $ = require("jquery-1.8.2.min");

    var defaults = {
        speed:1000,
        pause:4000,
        showItems:1,
        animation:"fade",
        mousePause:true,
        isPaused:false,
        direction:"up",
        height:0
    };

    function hotRoller(elem,options){
        this.$elm = $(elem);
        this.opts = $.extend({},defaults,options);

        this.init();
    }

    hotRoller.prototype = {
        init:function(){
            var itemH = 0,
                that = this;

            if(this.opts.height == 0){
                this.$elm.find("ul li").each(function(){
                    if($(this).height() > itemH){
                        itemH = $(this).outerHeight();
                    }
                });
                this.$elm.height(itemH * this.opts.showItems)
            }else{
                this.$elm.height(this.opts.height)
            }

            var rollerTime = setInterval(function(){
                if(that.opts.direction == 'up'){
                    that.moveUp(itemH)
                }else{
                    that.moveDown(itemH)
                }
            },that.opts.pause);

            if(this.opts.mousePause){
                this.$elm.bind('mouseenter',function(){
                    that.opts.isPaused = true;
                }).bind("mouseleave",function(){
                        that.opts.isPaused = false;
                    })
            }
        },
        moveUp:function(itemH){
            if(this.opts.isPaused){
                return;
            }
            var $ul = this.$elm.children("ul");
            var $cloneLi =  $ul.children("li:first").clone(true);
            if(this.opts.height > 0){
                itemH = $ul.children("li:first").height()
            }
            $ul.animate({top:"-="+itemH + "px"},this.opts.speed,function(){
                $(this).children("li:first").remove();
                $(this).css("top","0px")
            });
            if (this.opts.animation == "fade") {
                $ul.children("li:first").fadeOut(this.opts.speed);
                if (this.opts.height == 0) {
                    $ul.children("li:eq(" + this.opts.showItems + ")").hide().fadeIn(this.opts.speed)
                }
            }
            $cloneLi.appendTo($ul)
        },
        moveDown:function(itemH){
            if (this.opts.isPaused) {
                return
            }
            var $ul = this.$elm.children("ul");
            var $cloneLi = $ul.children("li:last").clone(true);
            if (this.opts.height > 0) {
                itemH = $ul.children("li:first").height()
            }
            $ul.css("top", "-" + itemH + "px").prepend($cloneLi);
            $ul.animate({top:0}, this.opts.speed, function () {
                $(this).children("li:last").remove()
            });
            if (this.opts.animation == "fade") {
                if (this.opts.height == 0) {
                    $ul.children("li:eq(" + this.opts.showItems + ")").fadeOut(this.opts.speed)
                }
                $ul.children("li:first").hide().fadeIn(this.opts.speed)
            }
        }
    };
    return hotRoller;
});